var baseUrl = 'https://api.qikuai100.com/';
$(function() {
	$("#sendCode").click(function() {
		sendCode($("#sendCode"));
	});
});

// 下单
function getOrder() {
	var name = $('#name').val(),
		address = $("#addressChoose").val(),
		phone = $('#phone').val(),
		codeNum = $('#codeNum').val();
	var result = isPhoneNum(); //判断手机号码是不是正确的
	if($.trim(name) == '') {
		layer.msg("请输入姓名！");
		return;
	}
	if($.trim(address) == '') {
		layer.msg("请选择地址！");
		return;
	}
	if($.trim(codeNum) == '') {
		layer.msg("请输入手机验证码！");
		return;
	}
	if(result) {
		$.ajax({
			type: "POST",
			url: baseUrl + "api/consult/add",
			data: {
				name: name,
				address: address,
				phone: phone,
				captcha: codeNum
			},
			success: function(result) {
				//				console.log(result);
				if(result.ret == 0) {
					window.location.href = '/index/index';
					layer.msg("我们工作人员会尽快联系您，请耐心等待！");
				} else {
					layer.msg(result.res_info);
				}
			}
		});
	}
}

function sendCode(obj) {
	var phone = $('#phone').val();
	var result = isPhoneNum(); //判断手机号码是不是正确的
	if(result) {
		$.ajax({
			type: "POST",
			url: baseUrl + "api/captcha/consult",
			data: {
				"phone": phone
			},
			success: function(result) {
				if(result.ret == 0) {
					settime(obj);
				} else {
					layer.msg(result.res_info);
				}
			}
		});
	}
}
//开始倒计时
var countdown = 60;

function settime(obj) {
	if(countdown == 0) {
		obj.removeClass("disabledStyle");
		obj.removeAttr("disabled");
		obj.val("获取验证码");
		return;
	} else {
		obj.addClass("disabledStyle");
		obj.attr("disabled", true);
		obj.val(countdown + "S");
		countdown--;
	}
	setTimeout(function() {
		settime(obj);
	}, 1000);
}
//校验手机号是否合法
function isPhoneNum() {
	var phone = $('#phone').val();
	var myreg = /^1[34578]\d{9}$/;
	if(!myreg.test(phone)) {
		layer.msg('请输入有效的手机号码');
		return false;
	} else {
		return true;
	}
}